package com.muqi.app.qlearn.mine;

import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

import com.muqi.app.network.http.AsyncCacheHttpForGet;
import com.muqi.app.network.http.AsyncCacheHttpForGet.LoadOverListener;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.NetWorkUtils;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.ShowToast;
import com.muqi.app.qlearn.adapter.AddressAdapter;
import com.muqi.app.qlearn.adapter.AddressAdapter.onCheckListener;
import com.muqi.app.qlearn.modles.AddressBean;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.R;
import com.wenhui.pedant.SweetAlert.SweetAlertDialog;
import com.wenhui.pedant.SweetAlert.SweetAlertDialog.OnSweetClickListener;

public class AddressManageActivity extends BaseFragmentActivity implements LoadOverListener, 
	OnClickListener, OnItemClickListener, OnItemLongClickListener, onCheckListener {
	public static final String ADDRESSTYPE = "address_type";
	private ListView mListview;
	private AddressAdapter mAdapter;
	private List<AddressBean> dataList;
	private String addressType = "manage_address";
	private String checkedId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aty_address_manage);
	}

	@Override
	protected void onInit() {
		mListview = (ListView) findViewById(R.id.listview);
		mListview.setOnItemClickListener(this);
		mListview.setOnItemLongClickListener(this);
		findViewById(R.id.addAddress).setOnClickListener(this);
	}

	@Override
	protected void onData() {
		addressType = getIntent().getStringExtra(ADDRESSTYPE);
		if (addressType != null) {
			if (addressType.equals("select_address")) {
				checkedId = getIntent().getStringExtra("checkedId");
			}
		}
		
		loadingData(); 
	}

	private void loadingData() {
		HashMap<String, Object> getMap = new HashMap<String, Object>();
		getMap.put("token", mSpUtil.getToken());
		String requestUrl = ParamsUtils.buildParams(NetWorkApi.GET_USER_ADDRESSLIST_API, getMap);
		AsyncCacheHttpForGet.getInstance(this);
		AsyncCacheHttpForGet.getCacheHttp(requestUrl, this);
	}

	public void finish(View view) {
		finish();
	}

	@Override
	public void onOverResult(String request, String result) {
		dataList = ResponseUtils.getAddressList(this, result);
		if (dataList != null && dataList.size() > 0) {
			hidEmpty();
			showList();
		}else{
			if (dataList != null) {
				if (dataList.size() == 0) {
					showEmpty("还没有设置任何地址");
				}
			}

		}
	}

	private void showList() {
		if (mAdapter != null) {
			mAdapter=null;
		}
		 mAdapter = new AddressAdapter(this, dataList, addressType);
		 if (addressType!=null && addressType.equals("select_address")
				 &&!TextUtils.isEmpty(checkedId)) {
			 mAdapter.setCheckId(checkedId);
		 }
		 mAdapter.setCheckListener(this);
		 mListview.setAdapter(mAdapter);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.addAddress:
			Intent intent = new Intent(this, AddressNewActivity.class);
			startActivityForResult(intent, 100);
			break;
		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent data) {
		super.onActivityResult(arg0, arg1, data);
		if (arg1 == RESULT_OK && data != null) {
			loadingData();
		}
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
		new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE).setTitleText("是否要删除条目").setCancelText("取消").setConfirmText("确定").showCancelButton(true).setCancelClickListener(new OnSweetClickListener() {
			@Override
			public void onClick(SweetAlertDialog mDialog) {
				mDialog.dismissWithAnimation();
			}
		}).setConfirmClickListener(new OnSweetClickListener() {
			@Override
			public void onClick(SweetAlertDialog mDialog) {
				mDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
				mDialog.setTitleText("正在删除记录").showCancelButton(false).setCancelable(false);
				mDialog.changeAlertType(SweetAlertDialog.PROGRESS_TYPE);
				loadingDelete(position, mDialog);
			}
		}).show();

		return true;
	}

	protected void loadingDelete(final int position, final SweetAlertDialog mDialog) {
		if (NetWorkUtils.isNetworkAvailable(this)) {
			HashMap<String, Object> voteMap = new HashMap<String, Object>();
			voteMap.put("token", mSpUtil.getToken());
			voteMap.put("dizhi_id", dataList.get(position).getDizhi_id());
			String requestApi = ParamsUtils.buildParams(NetWorkApi.DELETE_USER_ADDRESS_API, voteMap);
			MyAsyncHttp.get(this, requestApi, new AsyncCallBackListener() {
				
				@Override
				public void onAsyncCallBack(RESULTCODE resuleCode, String request,String result) {
					if(resuleCode == RESULTCODE.SUCCESS){
						mDialog.dismissWithAnimation();
						dataList.remove(position);
						if(dataList.size()<=0){
							showEmpty("还没有设置任何地址");
							return ;
						}else{
							hideLoading();
						}
						if (mAdapter != null)
							mAdapter.notifyDataSetChanged();
					}
				}
			});
		} else {
			ShowToast.showShort(this, R.string.net_break);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent(this, AddressNewActivity.class);
		intent.putExtra(AddressNewActivity.ADDRESSDATA, dataList.get(position));
		startActivityForResult(intent, 200);
	}

	@Override
	public void onCheck(int position) {
		mAdapter.setCheckId(dataList.get(position).getDizhi_id());
		mAdapter.notifyDataSetChanged();

		Intent intent = getIntent();
		intent.putExtra("AddressInfo", dataList.get(position));
		setResult(RESULT_OK, intent);

		finish();
	}

//	@Override
//	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//		AddressBean data = (AddressBean) buttonView.getTag();
//		if (isChecked) {
//			mAdapter.setCheckId(data.getDizhi_id());
//			mAdapter.notifyDataSetChanged();
//
//			Intent intent = getIntent();
//			intent.putExtra("AddressInfo", data);
//			setResult(RESULT_OK, intent);
//
//			finish();
//		} else {
//			if (TextUtils.equals(data.getDizhi_id(), mAdapter.getCheckId())) {
//				mAdapter.setCheckId(data.getDizhi_id());
//				mAdapter.notifyDataSetChanged();
//				Intent intent = getIntent();
//				intent.putExtra("sel_addr", data);
//				setResult(RESULT_OK, intent);
//				
//				finish();
//			}
//		}
//	}

}
